package com.vdf.sisconsolid.persistencemodule.converter;

import java.util.ArrayList;
import java.util.List;

import com.vdf.sisconsolid.persistencemodule.dto.ConsolidatorDTO;
import com.vdf.sisconsolid.persistencemodule.dto.KeyValueBean;
import com.vdf.sisconsolid.persistencemodule.model.Consolidator;
import com.vdf.sisconsolid.persistencemodule.model.Person;

public class ConsolidatorConverter {

	public static List<KeyValueBean> toConsolidatorKeyValue(
			List<Consolidator> consolidatorList) {
		List<KeyValueBean> consolidatorBeanList = new ArrayList<KeyValueBean>();
		for (Consolidator consolidator : consolidatorList) {
			KeyValueBean pBean = new KeyValueBean();
			pBean.setKey(consolidator.getId());
			pBean.setValue(consolidator.getPerson().getFirstName() + " "
					+ consolidator.getPerson().getLastName());

			consolidatorBeanList.add(pBean);
		}
		return consolidatorBeanList;
	}

	
	public static List<ConsolidatorDTO> toDTOList(List<Consolidator> consolidators) {
		List<ConsolidatorDTO> consolidatorDTOList = new ArrayList<ConsolidatorDTO>();
		for (Consolidator consolidator : consolidators) {
			consolidatorDTOList.add(toDTO(consolidator));
		}
		return consolidatorDTOList;
	}

	public static ConsolidatorDTO toDTO(Consolidator consolidator) {		
		ConsolidatorDTO cDTO = new ConsolidatorDTO();
		cDTO.setIdConsolidator(consolidator.getId());
		cDTO.setIdFamily(consolidator.getFamily().getId());
		cDTO.setFamily(consolidator.getFamily().getName());

		Person person = consolidator.getPerson();;
		cDTO.setIdPerson(person.getId());
		cDTO.setDocument(person.getDocument());
		cDTO.setEmail(person.getEmail());
		cDTO.setFirstName(person.getFirstName());
		cDTO.setLastName(person.getLastName());
		cDTO.setPersonalPhone(person.getPersonalPhone());
		return cDTO;
	}
	
	public static Consolidator toEntity(ConsolidatorDTO cDTO) {

		// consolidator
		Consolidator consolidator = new Consolidator();
		consolidator.setId(cDTO.getIdConsolidator());
		consolidator.setIdFamily(cDTO.getIdFamily());

		Person person = new Person();
		person.setId(cDTO.getIdPerson());
		person.setDocument(cDTO.getDocument());
		person.setEmail(cDTO.getEmail());
		person.setFirstName(cDTO.getFirstName());
		person.setLastName(cDTO.getLastName());
		person.setPersonalPhone(cDTO.getPersonalPhone());
		consolidator.setPerson(person);

		return consolidator;
	}

}
